//
//  ZDPoiController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/18.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDPoiController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import "ZDAnnotationView.h"

@interface ZDPoiController ()<AMapSearchDelegate,MAMapViewDelegate>
/**
 搜索对象
 */
@property(nonatomic, strong) AMapSearchAPI *search;
/**
 <#Description#>
 */
@property(nonatomic, strong) MAMapView *mapView;

@end

@implementation ZDPoiController

- (void)setupUI
{
    [super setupUI];
   
    ///初始化地图
    _mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    _mapView.showsCompass = NO;
    _mapView.showsScale = NO;
    _mapView.delegate = self;
    
    ///把地图添加至view
    [self.view addSubview:_mapView];
    
    ///显示小蓝色的点
    _mapView.showsUserLocation = YES;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    
    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
    
    request.location            = [AMapGeoPoint locationWithLatitude:36.67322944 longitude:117.12706434];
    request.keywords            = self.poiString;
    /* 按照距离排序. */
    request.sortrule            = 0;
    request.requireExtension    = YES;
    
    //创建搜索
    self.search = [[AMapSearchAPI alloc] init];
    self.search.delegate = self;
    [self.search AMapPOIAroundSearch:request];

}
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
    if (response.pois.count == 0)
    {
        return;
    }
    
    //解析response获取POI信息，具体解析见 Demo
    NSLog(@"%s---%d---%@",__func__,__LINE__,response);
    NSArray *array = response.pois;
    for (AMapPOI *poi in array) {
        
        MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
        pointAnnotation.coordinate = CLLocationCoordinate2DMake(poi.location.latitude, poi.location.longitude);
        pointAnnotation.title = poi.name;
        pointAnnotation.subtitle = poi.address;
        
        [_mapView addAnnotation:pointAnnotation];
    }
}


/**
 设置大头针的样式
 
 @param mapView <#mapView description#>
 @param annotation <#annotation description#>
 @return <#return value description#>
 */
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        ZDAnnotationView *annotationView = (ZDAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil)
        {
            annotationView = [[ZDAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.canShowCallout= YES;       //设置气泡可以弹出，默认为NO
        annotationView.poiString = self.poiString;
        return annotationView;
    }
    return nil;
}


@end
